import cclong from '../utils/globalfun'

function clearAll({selectBrandList, selectFemaleCateList, selectManCateList, otherPrams, price}) {
  if (!cclong.isArrayEmpty(selectBrandList)) {
    for (let i = 0; i < selectBrandList.length; i++) {
      const brandItem = selectBrandList[i]
      brandItem.checked = false
    }
    selectBrandList.splice(0, selectBrandList.length)
  }
  if (!cclong.isArrayEmpty(selectFemaleCateList)) {
    for (let i = 0; i < selectFemaleCateList.length; i++) {
      const cateItem = selectFemaleCateList[i]
      cateItem.checked = false
    }
    selectFemaleCateList.splice(0, selectFemaleCateList.length)
  }
  if (!cclong.isArrayEmpty(selectManCateList)) {
    for (let i = 0; i < selectManCateList.length; i++) {
      const cateItem = selectManCateList[i]
      cateItem.checked = false
    }
    selectManCateList.splice(0, selectManCateList.length)
  }
  if (!cclong.isNull(otherPrams)) {
    for (let item in otherPrams) {
      const conditionItem = otherPrams[item]
      if (!cclong.isArrayEmpty(conditionItem)) {
        for (let i = 0; i < conditionItem.length; i++) {
          const cateItem = conditionItem[i]
          cateItem.checked = false
        }
        conditionItem.splice(0, conditionItem.length)
        otherPrams[item] = conditionItem
      }
    }
  }
  if (!cclong.isNull(price)) {
    price.min_price = ''
    price.max_price = ''
    price.min_discount = 1.0
    price.max_discount = 10.0
    price.leftValue = 1
    price.rightValue = 10
  }
}

function getCateCondition(selectFemaleCateList, selectManCateList) {
  let cateCondition = new Array();
  let params = {}
  let gender = ''
  if (!cclong.isArrayEmpty(selectFemaleCateList)) {
    cateCondition = cateCondition.concat(selectFemaleCateList);
    gender = '1'
  }
  if (!cclong.isArrayEmpty(selectManCateList)) {
    cateCondition = cateCondition.concat(selectManCateList);
    gender = cclong.isEmpty(gender) ? '2' : ''
  }
  let simplifyCondtion = new Array();
  if (!cclong.isArrayEmpty(cateCondition)) {
    for (let i = 0; i < cateCondition.length; i++) {
      const item = cateCondition[i];
      simplifyCondtion[i] = item.value;
    }
  }
  params.gender = gender
  params.cate = simplifyCondtion
  return params;
}

function getOtherCondition(otherPrams, paramsObj) {
  if (cclong.isArrayEmpty(otherPrams)) {
    return paramsObj
  }
  for (var key in otherPrams) {
    let itemArray = otherPrams[key]
    if (!cclong.isArrayEmpty(itemArray)) {
      let params = new Array()
      for (let i = 0; i < itemArray.length; i++) {
        params = params.concat(itemArray[i].value)
      }
      paramsObj[key] = params
    }
  }
  return paramsObj
}

function getAllCondition({ selectBrandList, selectFemaleCateList, selectManCateList, otherPrams, price, word, sort_type, sort_id, page_index, page_size, promotion_id}) {
  console.log(selectBrandList)
  const cateCondition = getCateCondition(selectFemaleCateList, selectManCateList);
  const brandCondition = getBrandCondition(selectBrandList);
  let paramsObj = {};
  if (!cclong.isNull(cateCondition)) {
    paramsObj.category_ids = cateCondition.cate;
    paramsObj.gender = cateCondition.gender;
  }
  if (!cclong.isArrayEmpty(brandCondition)) {
    paramsObj.designer_ids = brandCondition;
  }
  if (!cclong.isNull(price)) {
    if (!cclong.isEmpty(price.min_price)) {
      paramsObj.min_price = price.min_price;
    }
    if (!cclong.isEmpty(price.max_price)) {
      paramsObj.max_price = price.max_price;
    }
    if (!cclong.isEmpty(price.min_discount)) {
      paramsObj.min_discount = price.min_discount;
    }
    if (!cclong.isEmpty(price.max_discount)) {
      paramsObj.max_discount = price.max_discount;
    }
  }
  paramsObj = getOtherCondition(otherPrams, paramsObj)
  paramsObj.tag = word;
  paramsObj.sort_id = sort_id;
  paramsObj.sort_type = sort_type;
  paramsObj.promotion_id = cclong.isEmpty(promotion_id) ? '' : promotion_id;
  paramsObj.page_index = page_index == 0 ? 1 : page_index;
  paramsObj.page_size = page_size == 0 ? 10 : page_size;
  return paramsObj
}

function getBrandCondition(selectBrandList) {
  let simplifyCondtion = new Array();
  if (!cclong.isArrayEmpty(selectBrandList)) {
    for (let i = 0; i < selectBrandList.length; i++) {
      const item = selectBrandList[i];
      simplifyCondtion[i] = item.value;
    }
  }
  return simplifyCondtion;
}

export default {
  getAllCondition,
  clearAll
}

